package com.yfp.goods.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.yfp.common.core.constant.DbConstants;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.exception.ServiceException;
import com.yfp.common.core.utils.bean.BeanUtils;
import com.yfp.goods.domain.*;
import com.yfp.goods.domain.dto.*;
import com.yfp.goods.domain.dto.*;
import com.yfp.goods.mapper.AppletMapper;
import com.yfp.goods.mapper.ComboMapper;
import com.yfp.goods.mapper.GoodsNutritiveMapper;
import com.yfp.goods.service.AppletService;
import com.yfp.goods.service.ComboDetailImgService;
import com.yfp.goods.service.ComboGoodsService;
import com.yfp.goods.service.TagComboService;
import com.yfp.goods.domain.*;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service("appletService")
public class AppletServiceImpl implements AppletService {

    @Resource
    private AppletMapper appletMapper;

    @Resource
    private ComboMapper comboMapper;

    @Resource
    private ComboDetailImgService comboDetailImgService;

    @Resource
    private ComboGoodsService comboGoodsService;

    @Resource
    private GoodsNutritiveMapper goodsNutritiveMapper;

    @Resource
    private TagComboService tagComboService;

    /**
     * 获取套餐的标签接口信息
     *
     * @param word 关键字
     * @return {@link R}
     */
    @Override
    public List<LabelDto> comboLabelSearchBasicInfo(String word) {
        return this.appletMapper.queryLabelListByWord(word);
    }

    /**
     * 筛选套餐基础数据
     *
     * @param word 单词
     * @return {@link R}
     */
    @Override
    public List<AppletGoodsDto> comboLimitBasicInfo(String word) {
        return this.appletMapper.comboLimitBasicInfo(word);
    }

    /**
     * 搜索套餐名称
     *
     * @param word 单词
     * @return {@link R}
     */
    @Override
    public List<GoodsSearchNameDto> goodsSearchName(String word) {
        return this.appletMapper.goodsSearchName(word);
    }

    /**
     * 根据Id获取套餐名称
     *
     * @param comboId 组合id
     * @return {@link String}
     */
    @Override
    public String comboNameById(String comboId) {
        return this.appletMapper.comboNameById(comboId);
    }

    /**
     * 根据Id获取套餐详细信息
     *
     * @param comboId 组合id
     * @return {@link AppletComboInfoDto}
     */
    @Override
    public AppletComboInfoDto comboUserViewById(String comboId) {
        AppletComboInfoDto appletComboInfoDto = new AppletComboInfoDto();
        Combo combo = this.comboMapper.queryById(comboId);
        if (Objects.isNull(combo)) throw new ServiceException("套餐不存在");
        appletComboInfoDto.setComboName(combo.getName());
        appletComboInfoDto.setExpireDay(combo.getExpireDay());
        appletComboInfoDto.setHeatTime(combo.getHeatTime());

        //背景图
        String resourceUrl = comboDetailImgService.getResourceUrl(Integer.valueOf(DbConstants.RESOURCE_TYPE_IMG), combo.getThumb());
        if (CharSequenceUtil.isNotEmpty(resourceUrl)) appletComboInfoDto.setThumbUrl(resourceUrl);

        //标签
        String tagId = combo.getTagId();

        if (CharSequenceUtil.isNotEmpty(tagId)) {
            List<String> tagIds = Arrays.stream(tagId.split(",")).toList();
            List<ComboLabel> comboLabelList = tagIds.stream().map(id -> {
                ComboLabel comboLabel = new ComboLabel();
                TagCombo tagCombo = this.tagComboService.queryById(id);
                if (Objects.nonNull(tagCombo)) {
                    comboLabel.setLabelName(tagCombo.getTagName());
                    return comboLabel;
                }
                return null;
            }).toList();
            appletComboInfoDto.setComboLabel(comboLabelList);
        }
        //价格
        Marketing marketing = new Marketing();
        marketing.setApplietAmount(combo.getMiniPrice());
        marketing.setVipAmount(combo.getVipPrice());
        marketing.setDeviceAmount(combo.getSalePrice());
        appletComboInfoDto.setMarketing(marketing);

        //资源
        List<ComboDetailImg> comboDetailImgs = comboDetailImgService.queryComboDetailImg(comboId);
        if (CollUtil.isNotEmpty(comboDetailImgs)) {
            List<ComboDetailData> comboDetailDataList = new ArrayList<>();
            for (ComboDetailImg comboDetailImg : comboDetailImgs) {
                if (Objects.equals(DbConstants.ImgType.COVER_IMG, comboDetailImg.getImgType())) continue;
                ComboDetailData comboDetailData = new ComboDetailData();
                comboDetailData.setResourceType(comboDetailImg.getResourceType());
                String resUrl = comboDetailImgService.getResourceUrl(comboDetailImg.getResourceType(), comboDetailImg.getResourceId());
                if (CharSequenceUtil.isNotEmpty(resUrl)) comboDetailData.setResourceUrl(resUrl);
                comboDetailDataList.add(comboDetailData);
            }
            appletComboInfoDto.setComboDetailData(comboDetailDataList);
        }

        //套餐商品
        List<Goods> goodsList = comboGoodsService.queryByComboId(comboId);
        if (CollUtil.isNotEmpty(goodsList)) {
            List<GoodsData> goodsDataList = new ArrayList<>();
            for (Goods goods : goodsList) {
                GoodsData goodsData = new GoodsData();
                goodsData.setGoodsName(goods.getGoodsName());
                // todo 味道改为从combo取值 可能没用到，没动
                goodsData.setTaste(goods.getTaste());
                goodsData.setDosing(goods.getDosing());
                List<GoodsNutritive> goodsNutritives = this.goodsNutritiveMapper.queryById(goods.getGoodsId());
                if (CollUtil.isNotEmpty(goodsNutritives)) {
                    List<GoodsNutritiveDto> goodsNutritiveDtos = new ArrayList<>();
                    for (GoodsNutritive goodsNutritive : goodsNutritives) {
                        GoodsNutritiveDto goodsNutritiveDto = new GoodsNutritiveDto();
                        BeanUtils.copyBeanProp(goodsNutritiveDto, goodsNutritive);
                        goodsNutritiveDtos.add(goodsNutritiveDto);
                    }
                    goodsData.setGoodsNutritiveDtoList(goodsNutritiveDtos);
                }
                goodsDataList.add(goodsData);
            }
            // todo 味道改为从combo取值 可能没用到，没动
            Tastes tastes = new Tastes();
            goodsDataList.stream().map(GoodsData::getTaste)
                    .filter(CharSequenceUtil::isNotEmpty)
                    .map(Integer::valueOf)
                    .max(Integer::compareTo)
                    .ifPresent(tastes::setSpicy);
            appletComboInfoDto.setTastes(tastes);
        }
        return appletComboInfoDto;
    }
}
